﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using bdnt_pets_system_Entity;
using System.Windows.Data;
using bdnt_pets_system.Tools;
using System.Globalization;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.SystemViews
{
    public partial class DictInfoManagerControl : BaseUserControl
    {
        SystemServiceReference.SystemServiceClient sysClient;

        public DictInfoManagerControl()
        {
            InitializeComponent();
            sysClient = new SystemServiceReference.SystemServiceClient();
            InitEvent();
            QueryData();
        }

        void InitEvent()
        {
            sysClient.GetDictObjListConditionCompleted += new EventHandler<SystemServiceReference.GetDictObjListConditionCompletedEventArgs>(sysClient_GetDictObjListConditionCompleted);
            sysClient.DeleteDictObjCompleted += new EventHandler<SystemServiceReference.DeleteDictObjCompletedEventArgs>(sysClient_DeleteDictObjCompleted);
            btnNewDict.Click += new RoutedEventHandler(btnNewDict_Click);
            btnUpdateDict.Click += new RoutedEventHandler(btnUpdateDict_Click);
            btnDeleteDict.Click += new RoutedEventHandler(btnDeleteDict_Click);
        }

        void sysClient_DeleteDictObjCompleted(object sender, SystemServiceReference.DeleteDictObjCompletedEventArgs e)
        {
            if (e.Result)
                QueryData();
            else
                tbErrorMsg.SetBinding(TextBlock.TextProperty, new Binding("Strings.czsb") { Source = CuLc });
        }

        void sysClient_GetDictObjListConditionCompleted(object sender, SystemServiceReference.GetDictObjListConditionCompletedEventArgs e)
        {
            var cul = e.Result.ToList();
            DictInfoManager.AllDicts = e.Result;
            InitDicts();
        }

        void btnDeleteDict_Click(object sender, RoutedEventArgs e)
        {
            if (tvDict.SelectedItem == null)
            {
                tbErrorMsg.Text = MyStrings.qxzyscdx;// "请选择要删除的项！";
                //tbErrorMsg.SetBinding(TextBlock.TextProperty, new Binding("Strings.qxzsccd") { Source = CuLc });
                return;
            }
            if (MessageBox.Show(MyStrings.qdyscm,MyStrings.ts, MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                return;
            var item = (tvDict.SelectedItem as TreeViewItem).DataContext as Tbl_DictInfo;
            selectId = item.PId == 0 ? 1 : item.PId;
            isExpSelect = true;
            sysClient.DeleteDictObjAsync(item.Id);
        }

        void btnUpdateDict_Click(object sender, RoutedEventArgs e)
        {
            if (tvDict.SelectedItem != null)
            {
                var item = (tvDict.SelectedItem as TreeViewItem).DataContext as Tbl_DictInfo;
                var cuP = DictInfoManager.AllDicts.FirstOrDefault(p => p.Id == item.PId);
                item.PName = cuP != null ? cuP.DictName : "";
                var cupage = new NewDictInfoControl(item);
                App.ToPage(cupage);//跳转页面
            }
            else
            {
                tbErrorMsg.Text = MyStrings.qxzyxgdx;// "请选择要修改的项！";
            }
        }

        void btnNewDict_Click(object sender, RoutedEventArgs e)
        {
            Tbl_DictInfo cuObj = new Tbl_DictInfo();
            if (tvDict.SelectedItem != null)
            {
                var item = (tvDict.SelectedItem as TreeViewItem).DataContext as Tbl_DictInfo;
                cuObj.PId = item.Id;
                cuObj.PName = item.DictName;
            }
            var cupage = new NewDictInfoControl(cuObj);
            App.ToPage(cupage);//跳转页面
        }

        void QueryData()
        {
            sysClient.GetDictObjListConditionAsync("");
        }

        public int selectId = 1;
        public bool isExpSelect = true;

        void InitDicts()
        {
            var allDicts = DictInfoManager.AllDicts;
            if (allDicts.Count == 0)
                return;
            tvDict.Items.Clear();

            expIds.Clear();
            GetExps(selectId);

            var pMs = allDicts.Where(p => p.PId == 0).OrderBy(p => p.Sequence);
            foreach (var item in pMs)
            {
                TreeViewItem cuI = new TreeViewItem();

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };

                TextBlock tb = new TextBlock() { FontSize = 12 };
                sp.Children.Add(tb);

                tb.Text = SessionManager.IsCN_ZH ? (item.DictName + "[" + MyStrings.ResourceManager.GetString("paixu")
                    + item.Sequence + "]:" + item.DictValue) : (item.en_DictName + "[" + MyStrings.ResourceManager.GetString("paixu")
                    + item.Sequence + "]:" + item.DictValue);

                cuI.Header = sp;
                cuI.DataContext = item;
                AddChildNode(cuI, item, allDicts);

                cuI.IsExpanded = expIds.Contains(item.Id) ? true : false;
                if (selectId == item.Id)
                {
                    cuI.IsSelected = true;
                    cuI.IsExpanded = isExpSelect;
                }

                tvDict.Items.Add(cuI);
            }
        }
        List<int> expIds = new List<int>();
        void GetExps(int selectId)
        {
            var allObjs = DictInfoManager.AllDicts;
            if (allObjs.Count == 0)
                return;
            var cuo = allObjs.FirstOrDefault(p => p.Id == selectId);
            if (cuo != null)
            {
                expIds.Add(cuo.PId);
                GetExps(cuo.PId);
            }
        }
        void AddChildNode(TreeViewItem pitem, Tbl_DictInfo pDict, ObservableCollection<Tbl_DictInfo> source)
        {
            var cuos = source.Where(c => c.PId == pDict.Id).OrderBy(p => p.Sequence);
            foreach (var item in cuos)
            {
                TreeViewItem cuI = new TreeViewItem();
                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };

                TextBlock tb = new TextBlock() { FontSize = 12 };
                sp.Children.Add(tb);

                tb.Text = SessionManager.IsCN_ZH ? (item.DictName + "[" + MyStrings.ResourceManager.GetString("paixu")
                    + item.Sequence + "]:" + item.DictValue) : (item.en_DictName + "[" + MyStrings.ResourceManager.GetString("paixu")
                    + item + "]:" + item.DictValue);

                cuI.Header = sp;
                cuI.DataContext = item;

                cuI.IsExpanded = expIds.Contains(item.Id) ? true : false;
                if (selectId == item.Id)
                {
                    cuI.IsSelected = true;
                    cuI.IsExpanded = isExpSelect;
                }
                pitem.Items.Add(cuI);
            }
        }
    }
}
